package com.atguigu.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.atguigu.gulimall.product.dao.BrandDao;
import com.atguigu.gulimall.product.dao.CategoryDao;
import com.atguigu.gulimall.product.entity.BrandEntity;
import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.vo.CateBrandRelationVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.product.entity.CategoryBrandRelationEntity;
import com.atguigu.gulimall.product.service.CategoryBrandRelationService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;

import javax.annotation.Resource;


/**
 * 品牌分类关联
 *
 * @author lxx
 * @email 1914264274@qq.com
 * @date 2022-03-20 14:31:59
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;
    @Resource
    BrandDao brandDao;
    @Resource
    CategoryDao categoryDao;

    /**
     * 查询关联数据
     */
    @RequestMapping(value = "/catelog/list", method = RequestMethod.GET)
    public R listRealation(@RequestParam("brandId") long brandId) {
        //根据品牌ID来查找关联的信息
        List<CategoryBrandRelationEntity> data = categoryBrandRelationService.list(
                new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id", brandId));
        return R.ok().put("data", data);
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = categoryBrandRelationService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 保存
     */
    @Transactional
    @RequestMapping("/save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation) {
        //在向表中正式添加数据之前先将brand_name和category_name查出来
        //获取品牌编号ID
        Long brandId = categoryBrandRelation.getBrandId();
        //获取菜单分类ID
        Long catelogId = categoryBrandRelation.getCatelogId();
        BrandEntity brandEntity = brandDao.selectById(brandId);
        CategoryEntity categoryEntity = categoryDao.selectById(catelogId);
        categoryBrandRelation.setBrandName(brandEntity.getName());
        categoryBrandRelation.setCatelogName(categoryEntity.getName());
        categoryBrandRelationService.save(categoryBrandRelation);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation) {
        categoryBrandRelationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

//    /product/categorybrandrelation/brands/list

    /**
     * 取分类关联的品牌
     * controller
     * 1）：接受请求，数据校验，组装数据
     * 2）：调用Service处理逻辑
     * 3）：组装返回的数据
     */
    @GetMapping("/brands/list")
    public R getBrands(@RequestParam(value = "catId", required = false) long catId) {
        List<BrandEntity> brands = categoryBrandRelationService.getBrands(catId);

        //数据转换
        List<CateBrandRelationVo> collect = brands.stream().map(item -> {
            CateBrandRelationVo cateBrandRelationVo = new CateBrandRelationVo();
            cateBrandRelationVo.setBrandId(item.getBrandId());
            cateBrandRelationVo.setBrandName(item.getName());
            return cateBrandRelationVo;
        }).collect(Collectors.toList());
        return R.ok().put("data", collect);
    }


}
